package org.jeecg.modules.demo.hd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.modules.demo.hd.entity.AppConfig;
import org.jeecg.modules.demo.hd.entity.AppVersion;
import org.jeecg.modules.demo.hd.service.IAppConfigService;
import org.jeecg.modules.demo.hd.service.IAppVersionService;
import org.jeecg.modules.demo.hd.vo.VersionVo;
import org.jeecg.modules.demo.hd.vo.WebPathVo;
import org.jeecg.modules.demo.weixin.WeixinAuth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "配置")
@RestController
@RequestMapping("/hd/appConfig")
@Slf4j
public class APPConfigController {

    @Autowired
    private IAppConfigService appConfigService;

    @Autowired
    private IAppVersionService appVersionService;

    /**
     * 获取提现配置
     */
    @ApiOperation(value = "获取提现配置", notes = "获取提现配置")
    @GetMapping(value = "/queryWithdrawalConfig")
    public Result<List<Integer>> queryWithdrawalConfig() {
        AppConfig config = appConfigService.getConfig();
        List<Integer> data = Arrays.stream(config.getWithdrawal().split(","))
                .mapToInt(Integer::parseInt).boxed().collect(Collectors.toList());
        return Result.OK(data);
    }

    @ApiOperation(value = "获取APP配置", notes = "获取APP配置")
    @GetMapping(value = "/getConfig")
    public Result<AppConfig>   getConfig() {
        AppConfig config = appConfigService.getConfig();
        return Result.OK(config);
    }

    @ApiOperation(value = "查看APP版本", notes = "查看APP版本")
    @GetMapping(value = "/version")
    public Result<VersionVo> version() {
        LambdaQueryWrapper<AppVersion> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AppVersion::getIsNew, "1");
        List<AppVersion> list = appVersionService.list(queryWrapper);
        if (list != null && list.size() > 0) {
            AppVersion appVersion = list.get(0);
            VersionVo vo = new VersionVo();
            vo.setVersionNmae(appVersion.getName());
            vo.setCode(appVersion.getCode());
            vo.setPath(appVersion.getPath());
            return Result.OK(vo);
        }
        VersionVo vo = new VersionVo();
        vo.setVersionNmae("1.1");
        vo.setCode("1");
        return Result.OK(vo);
    }


    @ApiOperation(value = "微信授权测试", notes = "微信授权测试")
    @GetMapping(value = "/wxAuth")
    public Result<String> wxAuth() {
        try {
            WeixinAuth.getAccessToken("", "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.OK("");
    }


    @ApiOperation(value = "现金兑换比例", notes = "现金兑换比例")
    @GetMapping(value = "/cashRate")
    public Result<String> cashRate() {
        AppConfig config = appConfigService.getConfig();
        return Result.OK(config.getCashRate() + "");
    }

    @ApiOperation(value = "工币兑换比例", notes = "工币兑换比例")
    @GetMapping(value = "/gbRate")
    public Result<String> gbRate() {
        AppConfig config =appConfigService.getConfig();
        return Result.OK(config.getGbRate() + "");
    }

    @ApiOperation(value = "获取web页面路径", notes = "获取web页面路径")
    @GetMapping(value = "/getWebPath")
    public Result<List<WebPathVo>> getWebPath() {
        List<WebPathVo> list = new ArrayList<>();
        AppConfig config = appConfigService.getConfig();
        list.add(new WebPathVo("公告", config.getGonggaoPath()));
        list.add(new WebPathVo("生态规划", config.getShengtaiguihuaPath()));
        list.add(new WebPathVo("先锋计划", config.getXianfengjihuaPath()));
        list.add(new WebPathVo("邀请好友", config.getYaoqinghaoyouPath()));
        list.add(new WebPathVo("实名认证", config.getShimingrenzhengPath()));
        list.add(new WebPathVo("关于我们", config.getGuanyuwomenPath()));
        list.add(new WebPathVo("隐私政策", config.getYinsizhengcePath()));
        list.add(new WebPathVo("我的工币", config.getWodegongbiPath()));
        list.add(new WebPathVo("我的现金", config.getWodegongbiPath()));
        return Result.OK(list);
    }

    /**
     * 分页列表查询
     *
     * @param appConfig
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "app_config-分页列表查询")
    @ApiOperation(value = "app_config-分页列表查询", notes = "app_config-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<AppConfig>> queryPageList(AppConfig appConfig,
                                                  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                  HttpServletRequest req) {
//        QueryWrapper<AppConfig> queryWrapper = QueryGenerator.initQueryWrapper(appConfig, req.getParameterMap());
        Page<AppConfig> page = new Page<AppConfig>(pageNo, pageSize);
        IPage<AppConfig> pageList = appConfigService.page(page, null);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param appConfig
     * @return
     */
    @AutoLog(value = "app_config-添加")
    @ApiOperation(value = "app_config-添加", notes = "app_config-添加")
    @RequiresPermissions("hd:app_config:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody AppConfig appConfig) {
        appConfigService.save(appConfig);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param appConfig
     * @return
     */
    @AutoLog(value = "app_config-编辑")
    @ApiOperation(value = "app_config-编辑", notes = "app_config-编辑")
    @RequiresPermissions("hd:app_config:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody AppConfig appConfig) {
        appConfigService.updateById(appConfig);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "app_config-通过id删除")
    @ApiOperation(value = "app_config-通过id删除", notes = "app_config-通过id删除")
    @RequiresPermissions("hd:app_config:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        appConfigService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "app_config-批量删除")
    @ApiOperation(value = "app_config-批量删除", notes = "app_config-批量删除")
    @RequiresPermissions("hd:app_config:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.appConfigService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "app_config-通过id查询")
    @ApiOperation(value = "app_config-通过id查询", notes = "app_config-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<AppConfig> queryById(@RequestParam(name = "id", required = true) String id) {
        AppConfig appConfig = appConfigService.getById(id);
        if (appConfig == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(appConfig);
    }

}
